package com.wfuhui.modules.oms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wfuhui.modules.oms.dao.UserAddressDao;
import com.wfuhui.modules.oms.entity.UserAddressEntity;
import com.wfuhui.modules.oms.service.UserAddressService;



@Service("userAddressService")
public class UserAddressServiceImpl implements UserAddressService {
	@Autowired
	private UserAddressDao userAddressDao;
	
	@Override
	public UserAddressEntity queryObject(Integer id){
		return userAddressDao.queryObject(id);
	}
	
	@Override
	public List<UserAddressEntity> queryList(Map<String, Object> map){
		return userAddressDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return userAddressDao.queryTotal(map);
	}
	
	@Override
	@Transactional
	public void save(UserAddressEntity userAddress){
		UserAddressEntity t = new UserAddressEntity();
		t.setUserId(userAddress.getUserId());
		t.setDft(false);
		userAddressDao.updateByUserId(t);
		userAddressDao.save(userAddress);
	}
	
	@Override
	public void update(UserAddressEntity userAddress){
		userAddressDao.update(userAddress);
	}
	
	@Override
	public void delete(Integer id){
		userAddressDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		userAddressDao.deleteBatch(ids);
	}

	@Override
	public UserAddressEntity queryByDefault(Long userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", userId);
		List<UserAddressEntity> userAddressList = userAddressDao.queryList(map);
		if(userAddressList != null && userAddressList.size() > 0) {
			return userAddressList.get(0);
		}
		return null;
	}
	
	@Override
	@Transactional
	public void updateDft(UserAddressEntity userAddress) {
		UserAddressEntity t = new UserAddressEntity();
		t.setUserId(userAddress.getUserId());
		t.setDft(false);
		userAddressDao.updateByUserId(t);
		userAddress.setUserId(null);
		userAddressDao.update(userAddress);
	}
	
}
